package com.itheima.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.cargo.*;
import com.itheima.domain.vo.ExportProductVo;
import com.itheima.domain.vo.ExportResult;
import com.itheima.domain.vo.ExportVo;
import com.itheima.service.cargo.ContractService;
import com.itheima.service.cargo.ExportProductService;
import com.itheima.service.cargo.ExportService;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/cargo/export")
public class ExportController extends BaseController {

    @Reference
    private ContractService contractService;

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    @RequestMapping(value = "/contractList")
    public String contractList(@RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "5") int size) {
        ContractExample example = new ContractExample();
        ContractExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(1).andCompanyIdEqualTo(companyId);
        PageInfo pageInfo = contractService.findAll(example, page, size);
        request.setAttribute("page", pageInfo);
        return "cargo/export/export-contractList";
    }

    @RequestMapping(value = "/toExport", name = "跳转报运页面")
    public String toExport(String id) {
        request.setAttribute("id", id);
        return "/cargo/export/export-toExport";
    }

    @RequestMapping(value = "/list", name = "分页查询出口报运列表")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size) {
        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo pageInfo = exportService.findAll(example, page, size);
        request.setAttribute("page", pageInfo);
        return "cargo/export/export-list";
    }

    @RequestMapping(value = "/edit", name = "报运保存")
    public String edit(Export export) {
        export.setCompanyId(companyId);
        export.setCompanyName(companyName);
        if (StringUtils.isEmpty(export.getId())) {
            export.setId(UUID.randomUUID().toString());
            exportService.save(export);
        } else {
            exportService.update(export);
        }
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/toView", name = "跳转报运单查看页面")
    public String toView(String id) {
        Export export = exportService.findById(id);
        request.setAttribute("export", export);
        return "cargo/export/export-view";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转报运编辑页面")
    public String toUpdate(String id) {
        Export export = exportService.findById(id);
        ExportProductExample example = new ExportProductExample();
        example.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> exportProduct = exportProductService.findAll(example);
        request.setAttribute("export", export);
        request.setAttribute("eps", exportProduct);
        return "cargo/export/export-update";
    }

    @RequestMapping(value = "/delete", name = "删除报运单")
    public String delete(String id) {
        exportService.delete(id);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "/exportE",name = "进行电子报运")
    public String exportE(String id){
        //1、构造传入的参数
        ExportVo exportVo = new ExportVo();

        //------------------------------
        //2、id是报运单的id
        //通过id查询export实体类
        Export export = exportService.findById(id);
        //3、将export实体类的信息复制到exportVo中
        BeanUtils.copyProperties(export, exportVo);
        exportVo.setExportId(id);
        //4、创建ExportProductVo列表
        List<ExportProductVo> exportProductVos = new ArrayList<ExportProductVo>();

        //5、通过报运单的id查询exportProduct列表
        ExportProductExample example = new ExportProductExample();
        ExportProductExample.Criteria criteria = example.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> exportProducts = exportProductService.findAll(example);

        for (ExportProduct exportProduct : exportProducts) {
            //6、循环报运货物列表，给exportProductVo赋值
            ExportProductVo exportProductVo = new ExportProductVo();
            BeanUtils.copyProperties(exportProduct, exportProductVo);
            exportProductVo.setExportId(id);
            exportProductVo.setExportProductId(exportProduct.getId());
            exportProductVos.add(exportProductVo);
        }
        //7、将ExportProductVo赋值给ExportVo.products
        exportVo.setProducts(exportProductVos);
        //------------------------------

        System.out.println("============给海关提供的参数："+exportVo);

        //目的：调用海关接口
        WebClient wc = WebClient.create("http://localhost:8088/ws/export/user");
        wc.post(exportVo);  //post方法里需要传入一个参数

        wc = wc.create("http://localhost:8088/ws/export/user/"+id);
        ExportResult exportResult = wc.get(ExportResult.class);

        System.out.println("=============海关返回的参数："+exportResult.toString());
        exportService.updateE(exportResult);

        return "redirect:/cargo/export/list.do";
    }
}
